<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px"
        >
          <el-form-item label="章节名称" prop="id">
            <el-cascader
              :options="catalogueDictList"
              :props="props"
              @change="handleChange"
              filterable
              style="width: 220px"
              @keyup.enter.native="handleQuery">
            </el-cascader>
          </el-form-item>
                    <el-form-item label="归属课程" prop="free">
                      <el-select filterable v-model="queryParams.courseId" placeholder="请选择课程" clearable style="width: 220px">
                        <el-option v-for="(item,index) in courseDictList" :key="index" :label="item.name"
                                   :value="item.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
          <el-form-item label="教师姓名" prop="teacherId">
            <el-select filterable v-model="queryParams.teacherId" placeholder="请选择教师" clearable style="width: 220px">
              <el-option v-for="(item,index) in teacherDictList" :key="index" :label="item.userName"
                         :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程类型" prop="typeId">
            <el-select filterable v-model="queryParams.typeId" placeholder="请选择课程类型" clearable style="width: 220px">
              <el-option v-for="(item,index) in courseTypeDictList" :key="item.id" :label="item.name"
                         :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="目录类型" prop="type">
            <el-select filterable v-model="queryParams.type" placeholder="请选择课程类型" clearable style="width: 220px">
              <el-option v-for="(item,index) in catalogueTypeList" :key="item.id" :label="item.name"
                         :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="创建时间" prop="createTimeList">
            <el-date-picker
              style="width: 220px"
              v-model="queryParams.createTimeList"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

        </el-row>

        <el-table v-loading="loading" :data="courseList">
          <el-table-column label="章节名称" align="center"  prop="name"/>
          <el-table-column label="课程名称" align="center" prop="courseName"/>
          <el-table-column label="教师名称" align="center" prop="teacherName"/>
          <el-table-column label="课程类型" align="center" prop="courseType"/>
          <el-table-column label="所属章节" align="center" prop="ParentName">
          <template slot-scope="scope">
            <span>{{scope.row.parentName||'-'}}</span>
          </template>
          </el-table-column>

          <el-table-column label="章节类型" align="center"  prop="type">
            <template slot-scope="scope">
              <span>{{ getType(scope.row.type) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width">
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:user:edit']"
              >修改
              </el-button>

              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:user:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          :total="page.total"
          :page.sync="page.current"
          :limit.sync="page.size"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <catalogue-add ref="catalogueAddRef" @refresh="refresh"/>
    <catalogue-update ref="catalogueUpdateRef" @refresh="refresh"/>

  </div>
</template>

<script>
import { teacherDict } from '@/api/xingyue/teacher'
import { courseTypeDict } from '@/api/xingyue/courseType'
import { addCourse,getCourseById,updateCourse,deleteCourse,courseDict } from '@/api/xingyue/course'
import { getToken } from '@/utils/auth'
import { parseTime } from '@/utils/ruoyi'
import CatalogueAdd from '@/views/xingyue/catalogueList/component/catalogue-add.vue'
import CatalogueUpdate from '@/views/xingyue/catalogueList/component/catalogue-update.vue'
import { getCatalogueInfo, catalogueDict, deleteCatalogue } from '@/api/xingyue/catalogue'

export default {
  name: 'User',
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: { CatalogueAdd,CatalogueUpdate },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      courseList: null,
      // 表单参数
      form: {},
      //   目录类型集合
      catalogueTypeList:[
        {id:1,name:"章"},
        {id:2,name:"节"},
        {id:3,name:"测试"},
      ],
      // 课程字典
      courseDictList:[],
      // 章节字典集合
      catalogueDictList:[],
      props: {   value: "id", label: "name",multiple: true,checkStrictly: true,emitPath:false },
      // 查询参数
      queryParams: {
        idList:undefined,
        courseId: undefined,
        teacherId: undefined,
        type: undefined,
        createTimeList: undefined
      },
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      // 教师字典集合
      teacherDictList: [],
      // 课程类型字典集合
      courseTypeDictList: [],
    }
  },
  created() {
    this.getList()
    this.getTeacherDict()
    this.getCourseTypeDict()
    this.getCatalogueDictList()
    this.getCourseDict()
  },
  computed: {
    getType() {
      return (type) => {
        if (type == 1) {
          return '章次'
        } else if (type ==2) {
          return '小节'
        } else if (type ==3) {
          return '测试'
        }else {
          return '_'
        }
      }
    },
  },
  methods: {
    /** 刷新页面*/
    refresh(){
      console.log("页面刷新了")
      this.getList()
    },
    /** 课程字典*/
    getCourseDict(){
      courseDict().then(response=>{
        if (response.code==200) {
          this.courseDictList = response.data
        }
      })
    },
    /** 章节字典*/
    getCatalogueDictList(){
      catalogueDict().then(response=>{
        if (response.code==200) {
          this.catalogueDictList = response.data
        }else {
          this.$message.error('服务器内部异常')
        }
      })
    },
    handleChange(value) {
      this.queryParams.idList=[]
      this.queryParams.idList = value
    },

    /** 课程类型字典接口*/
    getCourseTypeDict() {
      courseTypeDict().then(response => {
        if (response.code == 200) {
          this.courseTypeDictList = response.data
        } else {
          this.$message.error(response.msg || '服务器内部异常')
        }
      })
    },
    /** 教师字典接口 */
    getTeacherDict() {
      teacherDict().then(response => {
        if (response.code == 200) {
          this.teacherDictList = response.data
        } else {
          this.$message.error(response.msg || '服务器内部异常')
        }
      })
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true
      const data = {
        condition: this.queryParams,
        page: this.page
      }
      getCatalogueInfo(data).then(response => {
          this.courseList = response.data.list
          this.page.total = response.data.total
          this.loading = false
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$nextTick(()=>{
        this.$refs.catalogueAddRef.init()
      })

    },

    /** 修改按钮操作 */
    handleUpdate(row) {
     this.$refs.catalogueUpdateRef.init(row.id)
    },

    /** 提交按钮 */
    submitForm: function() {
      if (this.isUpdate==true) {
        updateCourse(this.form).then(response => {
          this.$modal.msgSuccess('修改成功')
          this.isUpdate=false
          this.open = false
          this.getList()
        })
      } else {
        addCourse(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除"' + row.name + '"的数据项？').then(function() {
        return deleteCatalogue(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 5px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

</style>
